import { ReplaySubject, AsyncSubject } from 'rxjs';

const ReplaySubjectDemo = () => {
// 保留最近n次事件数据，并在订阅时重播这些事件给订阅者
const source$ = new ReplaySubject(3)
source$.subscribe((data) =>
  console.log(`ReplaySubject 第一次订阅: ${data}`)
);
source$.next(1)
source$.next(2)
source$.subscribe(data => console.log(`ReplaySubject 第二次订阅: ${data}`))

source$.next(3);
source$.next(4);

source$.subscribe((data) =>
  console.log(`ReplaySubject 第三次订阅: ${data}`)
);

// AsyncSubject
const asyncsubject = new AsyncSubject()
asyncsubject.subscribe(data => console.log(`AsyncSubject 第一次订阅：${data}`))
asyncsubject.next(1)
asyncsubject.next(2)

asyncsubject.subscribe((data) =>
  console.log(`AsyncSubject 第二次订阅: ${data}`)
);
asyncsubject.next(3);
asyncsubject.next(4);

asyncsubject.subscribe((data) =>
  console.log(`AsyncSubject 第三次订阅: ${data}`)
);

asyncsubject.complete()

  return (
    <div>
      replay subject demo
    </div>
  )
}
export default ReplaySubjectDemo